package com.digitalchina.lxnsy.activity.management.message;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.MsgListVO;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.lzy.okhttputils.model.HttpParams;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management
 * 创建人：郑皓
 * 创建时间：2019/4/26 17:30
 * 修改备注：
 * ================================================
 */
public class MsgDetailsActivity extends BaseActivity {

    @Bind(R.id.layout)
    LinearLayout layout;
    @Bind(R.id.layout_tel)
    LinearLayout layoutTel;

    private String messageId = "";// msg ID
    private String tel = "";// 电话

    private MsgListVO vo;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_msg_detail);

    }

    @Override
    public void initMember() {

        vo = (MsgListVO) getIntent().getSerializableExtra("MsgListVO");
        messageId = vo.getMessageId();

        setTitle(vo.getMessageName());

        getMsgDetail();

    }

    @Override
    public void addListener() {
        layoutTel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!StringUtil.isStrEmpty(tel))
                    Utils.callPhone(activity, tel);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0: // 详情
                        callBack(message.obj);
                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                    case MSG_S_1:

                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {
        String json = "";
        try {
            String body = new JSONObject(object.toString()).getString("body");
            json = new JSONObject(body).getString("messageDetailVO");
            tel = new JSONObject(json).getString("mobilePhone");

        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (!StringUtil.isStrEmpty(json)) {
            Map maps = (Map) JSON.parse(json);
            for (Object map : maps.entrySet()) {
                String key = String.valueOf(((Map.Entry) map).getKey());
                String value = String.valueOf(((Map.Entry) map).getValue());
                if (!StringUtil.isStrEmpty(value) && !key.equals("messageType") && !key.equals("confirmStatus") && !key.equals("fertilizerPesticideId") &&
                        !key.equals("isLook")&& !key.equals("mobilePhone")) {
                    // 格式化时间
                    if (key.equals("commonDate")||key.equals("emergenceDate"))
                        value = value.substring(0, 10);
                    // 出入库类型 OUT 出库 ENTER 入库
                    if (key.equals("operationType")){
                        if(value.equals("OUT"))
                            value = "出库";
                        else
                            value = "入库";
                    }
                    // 浇水施肥类型 FERTILIZATION 施肥
                    if (key.equals("waterType")){
                        if(value.equals("FERTILIZATION"))
                            value = "施肥";
                        else
                            value = "浇水";
                    }
                    // 农资出入库类型 PESTICIDE 农药
                    if (key.equals("materialType")){
                        if(value.equals("PESTICIDE"))
                            value = "农药";
                        else
                            value = "施肥";
                    }

                    View view = LayoutInflater.from(this).inflate(R.layout.view_msg_details, layout, false);
                    TextView tvKey = view.findViewById(R.id.tv_key);
                    TextView tvValue = view.findViewById(R.id.tv_value);
                    tvKey.setText(switchKey(key));
                    tvValue.setText(value);
                    layout.addView(view);
                }
            }

            layoutTel.setVisibility(View.VISIBLE);
        }

    }

    private String switchKey(String key) {
        String name = "";
        switch (key) {
            case "commonDate":
                name = "日期";
                break;
            case "landParcelName":
                name = "地块信息";
                break;
            case "cropName":
                name = "农作物名称";
                break;
            case "varietyName":
                name = "品种名称";
                break;
            case "peopleNumber":
                name = "人数";
                break;
            case "hours":
                name = "工时";
                break;
            case "member":
                name = "社员";
                break;
            case "muNumber":
                name = "亩数";
                break;
            case "totalPrice":
                name = "总价";
                break;
            case "operateName":
                name = "操作人";
                break;
            case "productName":
                name = "农产品名称";
                break;
            case "productSpecs":
                name = "农产品规格";
                break;
            case "productNumber":
                name = "农产品数量";
                break;
            case "productPrice":
                name = "农产品单价";
                break;
            case "productTotalPrice":
                name = "农产品总价";
                break;
            case "feedSpecs":
                name = "种子规格";
                break;
            case "feedWeight":
                name = "种子重量（g）";
                break;
            case "feednumber":
                name = "种子数量";
                break;
            case "productsoutNumber":
                name = "农产品出库件数";
                break;
            case "productsoutWeight":
                name = "农产品出库重量（kg）";
                break;
            case "productsoutBatch":
                name = "农产品出库批次";
                break;
            case "seedlingBagNumber":
                name = "育苗袋数";
                break;
            case "acupointPlate":
                name = "穴盘";
                break;
            case "seedlingNumber":
                name = "育苗数量";
                break;
            case "colonizationNumber":
                name = "定植数量";
                break;
            case "waterType":
                name = "操作类型";
                break;
            case "pesticidesName":
                name = "农药名称";
                break;
            case "pesticidedosage":
                name = "农药使用药量";
                break;
            case "recoveryWeight":
                name = "采收重量";
                break;
            case "recoveryPackingSize":
                name = "采收包装规格";
                break;
            case "recoveryBatchNo":
                name = "采收批次信息";
                break;
            case "materialType":
                name = "农资出入库类型";
                break;
            case "operationType":
                name = "操作类型";
                break;
            case "outinbandSpecs":
                name = "农资出入库规格";
                break;
            case "outinbandWeight":
                name = "农资出入库重量";
                break;
            case "outinbandNumber":
                name = "农资出入库数量";
                break;
            case "fertilizerPesticideName":
                name = "农资出入库名称";
                break;
            case "emergenceDate":
                name = "出苗时间";
                break;
        }
        return name;
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public void getMsgDetail() {

        /**
         * messageId	消息ID
         * userAccId	用户ID
         */
        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        params.put("resNo", AdminXML.getResNo(activity));
        params.put("userAccId", AdminXML.getUserAccId(activity));
        params.put("messageId", messageId);

        OkHttpUtil.post(activity, URL.URL_GET_MESSAGE_DETAIL, "正在加载", params, mHandler,
                MSG_S_0, MSG_F_0);
    }
}
